package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.NotificationModel;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.widget.BMSDialog;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InfoNotifyAdapter extends BaseAdapter{

	private Context ctx;
	private LayoutInflater mInflater;
	private ArrayList<NotificationModel> mArrayList;
	private NotificationModel model = null;
	private BMSSharedPreferences preferences;
	private BMSListener mListener;

	public InfoNotifyAdapter(Context ctx, ArrayList<NotificationModel> mArrayList ,BMSListener listener ) {
		super();
		this.ctx = ctx;
		this.mInflater = LayoutInflater.from(ctx);
		this.mArrayList = mArrayList;
		this.preferences = new BMSSharedPreferences(ctx);
		this.mListener = listener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayList.size();
	}



	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		final ViewHolder holder;
		if (convertView == null){
			convertView = mInflater.inflate(R.layout.item_info_notify_layout,null);
			holder = new ViewHolder();
			holder.rlNotify = (RelativeLayout)convertView.findViewById(R.id.rl_info_notify);
			holder.cBox_notify = (CheckBox)convertView.findViewById(R.id.cBox_item_info_notify);
			holder.tvContent_notify = (TextView)convertView.findViewById(R.id.tv_item_info_notify_content);
			holder.tvPermission_notify = (TextView)convertView.findViewById(R.id.tv_item_info_notify_permission);
			holder.tvTime_notify = (TextView)convertView.findViewById(R.id.tv_item_info_notify_time);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		model = mArrayList.get(position);
		if(model!= null){
			holder.tvPermission_notify.setText(model.getUserFullname());
			holder.tvContent_notify.setText(model.getNotifyContent());
			holder.tvTime_notify.setText(Utils.convertTime(model.getNotifyTime()));
			holder.cBox_notify.setChecked(model.isChecked());
			if (model.isRead()){
				holder.tvContent_notify.setTextColor(ctx.getResources().getColor(R.color.hint_color));
				holder.tvContent_notify.setTypeface(null, Typeface.NORMAL);
			}else{
				holder.tvContent_notify.setTextColor(ctx.getResources().getColor(R.color.tv_color));
				holder.tvContent_notify.setTypeface(null, Typeface.BOLD);

			}
		}

		holder.cBox_notify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NotificationModel imodel = mArrayList.get(position);
				if (imodel.isChecked()){
					imodel.setChecked(false);
				}else{
					imodel.setChecked(true);
				}

			}
		});
		holder.rlNotify.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NotificationModel modelNOtify = mArrayList.get(position);
				if(model != null){
					BMSDialog dialog = new BMSDialog(ctx,modelNOtify);
					dialog.show();
					if (!modelNOtify.isRead())
						BMSCore.getInstance(ctx).taskSetReadNotify(modelNOtify.getNotifyId(), preferences.getUserAuthKey(ctx), mListener);
				}
			}
		});


		return convertView;
	}

	public ArrayList<NotificationModel> getListNotify(){

		return mArrayList;

	}

	class ViewHolder{
		private CheckBox cBox_notify;
		private RelativeLayout rlNotify;
		private TextView tvPermission_notify;
		private TextView tvContent_notify;
		private TextView tvTime_notify;
	}
}
